from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
import pymysql

pymysql.install_as_MySQLdb()
app = Flask(__name__)


# db = SQLAlchemy(app)

class Config(object):
    DEBUG = True
    SECRET_KEY = "*)asdfasd&fsadffasdf(^(#$#8423!#"
    SQLALCHEMY_DATABASE_URI = "mysql://root:cjl2012@127.0.0.1:3306/axfdb"
    # SQLALCHEMY_DATABASE_URI = 'sqlite:///db/datas.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = True  # 自动跟踪数据库数据修改
    SQLALCHEMY_ECHO = True


app.config.from_object(Config)
db = SQLAlchemy(app)


class Role(db.Model):
    __tablename__ = 'roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)

    # users = db.relationship('User', backref='role')  # []  r.users.append(u)  u

    def __repr__(self):
        return '<Role %s>' % self.name


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    role = db.relationship('Role', backref=db.backref("users", lazy='dynamic'))

    # role2 = db.relationship('Role', backref="users2")

    def __repr__(self):
        return '<User %s>' % self.name
